package com.school.controller.admin;

import com.school.common.ServiceResultEnum;
import com.school.common.TeaShopCategoryLevelEnum;
import com.school.entity.GoodsCategory;
import com.school.entity.IndexConfig;
import com.school.service.GoodsCategoryService;
import com.school.service.IndexConfigService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/admin")
public class GoodsCategoryController {
    @Resource
    private GoodsCategoryService goodsCategoryService;
    @RequestMapping("/categories")
    public String categories(HttpServletRequest request,
                             @RequestParam("categoryLevel") Byte categoryLevel,
                             @RequestParam("parentId") Long parentId,
                             @RequestParam("backParentId") Long backParentId){
        if(categoryLevel == null||parentId== null||backParentId==null){
            return "admin/error/error_404";
        }
        request.setAttribute("path", "tea_shop_category");
        request.setAttribute("parentId", parentId);
        request.setAttribute("backParentId", backParentId);
        request.setAttribute("categoryLevel", categoryLevel);
        return "admin/tea_shop_category";
    }
    @GetMapping("/categories/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params){
        if (StringUtils.isEmpty(params.get("page"))||
                StringUtils.isEmpty(params.get("limit"))||
                params.get("categoryLevel")== null||
                params.get("parentId")== null){
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        PageResult pageResult = goodsCategoryService.getCategorysPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }
    @PostMapping("/categories/save")
    @ResponseBody
    public Result save(@RequestBody GoodsCategory goodsCategory ){
        if(Objects.isNull(goodsCategory.getCategoryRank())||
                StringUtils.isEmpty(goodsCategory.getCategoryName())||
                Objects.isNull(goodsCategory. getParentId())||
                Objects.isNull(goodsCategory. getCategoryLevel())) {
            return ResultGenerator.genFailResult("参数异常");
        }
        String result=goodsCategoryService.saveGoodsCategory(goodsCategory);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult(result);
        }
    }
    @PostMapping("/categories/update")
    @ResponseBody
    public Result update(@RequestBody GoodsCategory goodsCategory){
        if(Objects.isNull(goodsCategory.getCategoryId())||
                StringUtils.isEmpty(goodsCategory.getCategoryName())||
                Objects.isNull(goodsCategory.getCategoryLevel())||
                Objects.isNull(goodsCategory.getParentId())||
                Objects.isNull(goodsCategory.getCategoryRank())) {
            return ResultGenerator.genFailResult("参数异常");
        }
        String result=goodsCategoryService.updateGoodsCategory(goodsCategory);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult(result);
        }
    }
    @PostMapping("/categories/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids){
        if(ids.length<1){
            return ResultGenerator.genFailResult("参数异常");
        }
        String result=goodsCategoryService.deleteBatch(ids);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult(result);
        }
    }
    @GetMapping("/categories/listForSelect")
    @ResponseBody
    public Result listForSelect(@RequestParam("categoryId") Long categoryId){
        if(categoryId==null||categoryId<1){
            return ResultGenerator.genFailResult("参数异常");
        }
        GoodsCategory goodsCategory=goodsCategoryService.getGoodsCategoryById(categoryId);
        if(goodsCategory==null||goodsCategory.getCategoryLevel()== TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel()){
            return ResultGenerator.genFailResult("参数异常");
        }
        Map categoryResult=new HashMap();
        if(goodsCategory.getCategoryLevel()==TeaShopCategoryLevelEnum.LEVEL_ONE.getLevel()){
            List<GoodsCategory> secondLevelCategories =goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(goodsCategory.getCategoryId()),TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel());
            if(!secondLevelCategories.isEmpty()){
                Long categoryId1 = secondLevelCategories.get(0).getCategoryId();
                List<GoodsCategory> thirdLevelCategories =goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId1),TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel());
                categoryResult.put("thirdLevelCategories",thirdLevelCategories);
                categoryResult.put("secondLevelCategories",secondLevelCategories);
            }
        }
        if(goodsCategory.getCategoryLevel()==TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel()){
            List<GoodsCategory> thirdLevelCategories =goodsCategoryService.selectByLevelAndParentIdAndNumber(Collections.singletonList(categoryId),TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel());
            categoryResult.put("thirdLevelCategories",thirdLevelCategories);
        }
       return ResultGenerator.genSuccessResult( categoryResult);
    }
}
